#include "ToolParameterDialog.h"
#include "../QSS.h"
#include <QScrollBar>
ToolParameterDialog::ToolParameterDialog(QWidget *parent)
        : QDialog(parent) {
    QVBoxLayout *layout = new QVBoxLayout(this);


    // 创建表格
    toolTable = new QTableWidget(this);
    toolTable->setColumnCount(4);
    toolTable->setRowCount(20); // 设置20行



// 设置表头标签
    toolTable->setHorizontalHeaderLabels(QStringList() << "刀具ID" << "刀具直径" << "刀具长度" << "刀具名称");
    layout->addWidget(toolTable);
    // 添加保存按钮
    QPushButton *saveButton = new QPushButton("Save", this);
    connect(saveButton, &QPushButton::clicked, this, &ToolParameterDialog::saveParameters);

    QHBoxLayout *buttonLayout = new QHBoxLayout(this);  //创建一个按钮容器 使用水平布局
    buttonLayout->addWidget(saveButton);


    // 添加删除按钮
    QPushButton *deleteButton = new QPushButton("All Delete", this);
    connect(deleteButton, &QPushButton::clicked, this, &ToolParameterDialog::deleteParameters);

    buttonLayout->addWidget(deleteButton);
    layout->addLayout(buttonLayout);


    setLayout(layout);
    setWindowTitle("输入刀具参数");
    resize(500, 600);  // 调整对话框大小

    loadParameters(); // 加载刀具参数
}

void ToolParameterDialog::loadParameters() {
    QFile file("tools.txt"); // 文件名
    if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        int row = 0;

        while (!in.atEnd() && row < toolTable->rowCount()) {
            QString line = in.readLine();
            QStringList parameters = line.split("; ");

            if (parameters.size() == 4) {
                // 设置表格内容
                toolTable->setItem(row, 0, new QTableWidgetItem(parameters[0].split(": ")[1]));
                toolTable->setItem(row, 1, new QTableWidgetItem(parameters[1].split(": ")[1]));
                toolTable->setItem(row, 2, new QTableWidgetItem(parameters[2].split(": ")[1]));
                toolTable->setItem(row, 3, new QTableWidgetItem(parameters[3].split(": ")[1]));
                row++;
            }
        }
        file.close();
    } else {
        QMessageBox::warning(this, "错误", "无法读取文件！");
    }
}

void ToolParameterDialog::saveParameters() {
    QFile file("tools.txt");  // 文件名
    if (file.open(QIODevice::WriteOnly| QIODevice::Text)) {
        QTextStream out(&file);
        for (int i = 0; i < toolTable->rowCount(); ++i) {
            QString toolID = toolTable->item(i, 0) ? toolTable->item(i, 0)->text() : "";
            QString toolRadius = toolTable->item(i, 1) ? toolTable->item(i, 1)->text() : "";
            QString toolLength = toolTable->item(i, 2) ? toolTable->item(i, 2)->text() : "";
            QString toolName = toolTable->item(i, 3) ? toolTable->item(i, 3)->text() : "";

            // 检查输入是否有效
            if (!toolID.isEmpty() && !toolRadius.isEmpty() && !toolLength.isEmpty() && !toolName.isEmpty()) {
                out << "ID: " << toolID << "; diameter: " << toolRadius
                    << "; length: " << toolLength << "; NAME: " << toolName << "\n";  // 写入文件
            }
        }
        file.close();
        accept();  // 关闭对话框
    } else {
        QMessageBox::warning(this, "错误", "无法保存文件！");
    }
}

void ToolParameterDialog::deleteParameters() {
    toolTable->clearContents(); // 清空表格内容
}


